// pages/meal_detail_oneday/meal_detail_oneday.js
const util = require("../../utils/util.js");
const getdata = require("../../tool/GetDate.js");
var app = getApp();
var currentPage = 0;
var listData = [];
var paramData = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hallArray: [],
    objectHallArray: [],
    hallID: 0,
    hallName: "",
    hallIndex: 0,
    mealArray: [],
    objectMealArray: [],
    mealID: 0,
    mealName: "",
    mealIndex: 0,
    date: "",
    mealList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    currentPage = 0;
    listData = [];
    this.getHall();
  },

  getHall: function() {
    let that = this;
    getdata.getdata({
      url: "gbt/BasicInfo/DiningHall/",
      data: app.globalData.myUserData.districtID,
      success: function(res) {
        console.log(res.root);
        if (res.root) {
          that.setData({
            hallArray: res.root.diningHallTitleArray,
            objectHallArray: res.root.diningHallArray,
            hallID: res.root.diningHallArray[0].id,
            hallName: res.root.diningHallArray[0].name,
            hallIndex: 0,
          });
        }
        //获取餐点信息
        that.getMeal();
      },
    });
  },

  getMeal: function() {
    let that = this;
    getdata.getdata({
      url: "gbt/BasicInfo/MealList/",
      data: app.globalData.myUserData.districtID,
      success: function(res) {
        console.log(res.root);
        if (res.root) {
          let array = [];
          for (let mealIndex = 0; mealIndex < res.root.data.length; mealIndex++) {
            array.push(res.root.data[mealIndex].name);
          }

          that.setData({
            mealArray: array,
            objectMealArray: res.root.data,
            mealID: res.root.data[0].id,
            mealName: res.root.data[0].name,
            mealIndex: 0,
            date: util.formatDateDiff2(1), //默认日期为明天
          });

          currentPage++;
          let param = {};
          param.hallID = that.data.hallID;
          param.hallName = that.data.hallName;
          param.mealName = res.root.data[0].name;
          param.startDate = util.formatDateDiff2(1);
          param.endDate = util.formatDateDiff2(1);
          param.currentPage = currentPage;
          param.districtID = app.globalData.myUserData.districtID;
          console.log(param);
          paramData = param;
          let data = JSON.stringify(paramData);
          that.getMealReportSelectDateAll(data);
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    currentPage++;
    paramData.currentPage = currentPage;
    let data = JSON.stringify(paramData);
    this.getMealReportSelectDateAll(data);
  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function() {

  // },

  //报餐统计（全部）
  getMealReportSelectDateAll: function(data) {
    let that = this;
    getdata.postdata({
      url: "gbt/Meal/MealReportSelectDateAll",
      data: data,
      success: function(res) {
        console.log(res.root);
        if (res.root) {
          let array = res.root.data;
          let mealItem = {};
          for (let index = 0; index < array.length; index++) {
            mealItem.titleName = array[index].name;
            mealItem.title = "工号";
            mealItem.value = array[index].number;
            mealItem.amount = array[index].qty;
            mealItem.currency = "份";
            listData.push(mealItem);
            mealItem = {};
          }
          console.log(listData);
          that.setData({
            mealList: listData,
          });
        }
      }
    });
  },

  bindHallPickerChange: function(e) {
    currentPage = 1;
    listData = [];
    let param = {};
    param.hallName = this.data.objectHallArray[e.detail.value].name;
    param.mealName = this.data.mealName;
    param.startDate = this.data.date;
    param.endDate = this.data.date;
    param.currentPage = currentPage;
    param.districtID = app.globalData.myUserData.districtID;
    paramData = param;
    let data = JSON.stringify(param);
    this.getMealReportSelectDateAll(data);

    this.setData({
      hallID: this.data.objectHallArray[e.detail.value].id,
      hallName: this.data.objectHallArray[e.detail.value].name,
      hallIndex: e.detail.value,
    });
  },

  onMealPickerChange: function(e) {
    console.log(e.detail);
    currentPage = 1;
    listData = [];
    let param = {};
    param.hallName = this.data.hallName;
    param.mealName = this.data.objectMealArray[e.detail.value].name;
    param.startDate = this.data.date;
    param.endDate = this.data.date;
    param.currentPage = currentPage;
    param.districtID = app.globalData.myUserData.districtID;
    paramData = param;
    let data = JSON.stringify(param);
    this.getMealReportSelectDateAll(data);

    this.setData({
      mealID: this.data.objectMealArray[e.detail.value].id,
      mealName: this.data.objectMealArray[e.detail.value].name,
      mealIndex: e.detail.value,
      date: util.formatDateDiff2(1), //默认日期为明天
    });
  },

  onDateChange: function(e) {
    currentPage = 1;
    listData = [];
    let param = {};
    param.hallName = this.data.hallName;
    param.mealName = this.data.mealName;
    param.startDate = e.detail.value;
    param.endDate = e.detail.value;
    param.currentPage = currentPage;
    param.districtID = app.globalData.myUserData.districtID;
    paramData = param;
    let data = JSON.stringify(param);
    console.log(data);
    this.getMealReportSelectDateAll(data);

    this.setData({
      date: e.detail.value,
    });
  },
})